<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        :key="item.com_id.toString()"
      >
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span style="color: #466b9d;" slot="title">{{ item.aut_name }}</span>
        <div slot="label">
          <p style="color: #363636;">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px;">{{ item.update | relativeTime }}</span>
            <van-button
              size="mini"
              type="default"
              @click="onReplyShow(item)"
            >回复</van-button>
          </p>
        </div>
        <van-icon
          slot="right-icon"
          :name="item.is_liking ? 'like' : 'like-o'"
          color="red"
          @click="onCommentLike(item)"
        />
      </van-cell>
    </van-list>
    <van-cell-group class="publish-wrap">
      <van-field
        clearable
        placeholder="请输入评论内容"
        v-model="inputComment"
      >
        <van-button
          slot="button"
          size="mini"
          type="info"
          @click="onAddComment"
        >发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->
    <!-- 评论回复 -->
    <!-- 组件中的弹层触发时层级最高 希望在dom渲染时以body为根节点 布局更为合理
          get-container="body" 即为弹层渲染时以body为根节点 -->
    <van-popup
      v-model="isReplyShow"
      get-container="body"
      round
      position="bottom"
      :style="{ height: '90%' }"
    >
    <comment-reply
      :comment="currentComment"
      v-if="isReplyShow"
      @close-reply="isReplyShow = false"
    ></comment-reply>
    </van-popup>
    <!-- 评论回复 -->
  </div>
</template>
<script>
import {
  getComment,
  addComment,
  deleteCommentLike,
  addCommentLike
} from '@/api/comment'
import commentReply from './comment-reply'

export default {
  data () {
    return {
      list: [],
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null, // 获取下一页评论数据的页码（偏移量）
      inputComment: '', // 评论框的内容
      isReplyShow: false,
      currentComment: {}
    }
  },
  components: {
    commentReply
  },
  methods: {
    async onLoad () {
      const res = await getComment({
        type: 'a', // 需要填写字符串
        source: this.$route.params.articleId, // 源id 文章id或评论id
        offset: this.offset
      })
      this.list.push(...res.data.data.results)
      this.loading = false
      const lastId = res.data.data.last_id
      if (lastId) {
        this.offset = lastId
      } else {
        this.finished = true
      }
    },
    async onAddComment () {
      const inputComment = this.inputComment.trim()
      if (!inputComment) {
        return
      }
      const res = await addComment({
        target: this.$route.params.articleId, // 评论的id
        content: inputComment // 评论内容
      })
      this.list.unshift(res.data.data.new_obj)
      this.inputComment = '' // 清空输入框
    },
    async onCommentLike (comment) {
      if (comment.is_liking) {
        await deleteCommentLike(comment.com_id)
      } else {
        await addCommentLike(comment.com_id)
      }
      comment.is_liking = !comment.is_liking
    },
    async onReplyShow (comment) {
      this.currentComment = comment
      this.isReplyShow = true
    }
  }
}
</script>
<style scoped lang='less'>
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}

.van-list {
  margin-bottom: 45px;
}
</style>
